package zoltan.balog.plan.impl;

import java.util.ArrayList;
import java.util.List;

import zoltan.balog.entities.City;
import zoltan.balog.entities.Route;
import zoltan.balog.plan.IPlan;

public class Plan implements IPlan {

	private final List<City> cities;
	private final boolean[][] exists;
	private final double[][] map;

	private final List<Route> routes;

	public Plan(final int numOfCities, final List<Route> routes) {
		cities = new ArrayList<City>(numOfCities);
		this.routes = routes;
		
		for (int i = 0; i < numOfCities; i++) {
			cities.add(new City(i));
		}

		map = new double[cities.size()][cities.size()];
		exists = new boolean[cities.size()][cities.size()];
		
		// init false
		initialize(numOfCities);

		for (final Route route : routes) {
			map[route.getFrom().getId()][route.getTo().getId()] = route.getCost();
			exists[route.getFrom().getId()][route.getTo().getId()] = true;
		}
	}

	private void initialize(final int numOfCities) {
		for (int i = 0; i < numOfCities; i++) {
			for (int j = 0; j < numOfCities; j++) {
				exists[i][j] = false;
			}
		}
	}

	@Override
	public boolean existRoute(final City city1, final City city2) {
		return exists[city1.getId()][city2.getId()];
	}

	@Override
	public List<City> getCities() {
		return cities;
	}

	@Override
	public int getNumOfCities() {
		return cities.size();
	}

	@Override
	public int getNumOfRoutes() {
		return routes.size();
	}

	@Override
	public List<Route> getRoutes() {
		return routes;
	}

	@Override
	public double routeCost(final City city1, final City city2) {
		return map[city1.getId()][city2.getId()];
	}

	@Override
	public String toString() {
		return "Plan Cities=" + cities.size() + System.getProperty("line.separator") + "[routes=" + routes + "]";
	}
}
